package com.itheima.wisdomnext.Exception;

import com.itheima.wisdomnext.entity.Result.ResponseResult;
import com.itheima.wisdomnext.entity.Result.enumcalss.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

@RestControllerAdvice
@Slf4j
public class BenGlobalExceptionHandler {
    // 正确的 logger 变量定义
    private static final Logger logger = LoggerFactory.getLogger(BenGlobalExceptionHandler.class);

    @ExceptionHandler(BusinessException.class)
    public ResponseResult businessExceptionHandler(BusinessException e) {
        logger.error("自定义异常：" + e.getMessage());
        return ResponseResult.failure(e.getMessage());  // 确保返回自定义的错误信息
    }


    @ExceptionHandler(Exception.class)
    public ResponseResult handleAllException(Exception e) {
        if (e instanceof AccessDeniedException) {
            logger.error("权限不足：" + e.getClass().getSimpleName() + e.getCause());
            return ResponseResult.failure("权限不足");
        } else if (e instanceof RuntimeException) {
            logger.error("运行时异常：" + e.getClass().getSimpleName() + e.getCause());
            return ResponseResult.failure("运行时异常");
        }
        logger.error("其他异常：" + e.getClass().getSimpleName() + e.getCause());
        return ResponseResult.failure("其他异常");
    }

}
